const connection = require('../app/database')
class avatar {
    //用户头像
    async saveavatar(filename, mimetype, size,userid) {
        const statement = `
        INSERT INTO avatar(filename,mimetype,size,user_id) VALUES(?,?,?,?) 
        `
        const data = await connection.execute(statement,[filename, mimetype, size,userid])  
        return data
    }
    async upavatarurl (url,id) {
        const statement = `UPDATE user set avatar_url = ? WHERE id = ?`
        const data = await connection.execute(statement,[url, id])  
        return data
      }
      async getAvatarByUserId (userid) {
        const statement = `SELECT * from avatar WHERE id = ?` 
        const data = await connection.execute(statement,[userid])
        return data[0][0]
      }


    //用户背景
   
    async upbgavatarurl(url,id) {
        const statement = `UPDATE user set bgimg = ? WHERE id = ?`
        const data = await connection.execute(statement,[url, id])  
        return data
    }
    async savebgimg(filename, mimetype, size,userid) {
        const statement = `
        INSERT INTO bgimg(filename,mimetype,size,user_id) VALUES(?,?,?,?) 
        `
        const data = await connection.execute(statement,[filename, mimetype, size,userid])  
        return data
    }
    async getBgimgByUserId (userid) {
        const statement = `SELECT * from avatar WHERE id = ?` 
        const data = await connection.execute(statement,[userid])
        return data[0][0]
    }
    async setfileinfo(filename, mimetype, userid,originalname) {
        const statement = `INSERT INTO file(file_name,file_type,user_id,or_name) VALUES (?,?,?,?)` 
        const data = await connection.execute(statement,[filename, mimetype, userid,originalname])
        return data 
    }
    async getfilenames (userid) {
        const statement = `SELECT or_name name , createAt,updateAt  from file WHERE user_id = ?` 
        const data = await connection.execute(statement,[userid])
        return data[0]
    }
    async getorfilename (filename,userid) {
        const statement = `SELECT file_name fileorname,file_type mimetype FROM file WHERE file.or_name = ? AND file.user_id = ?`
        const data  =await connection.execute(statement,[filename,userid])
        return data[0][0]
    }
    async deletefile (filename,id) {
        const statement = `DELETE FROM file WHERE file.or_name =?  AND file.user_id =? `
        const data  =await connection.execute(statement,[filename,id])
        return data[0]
    }
}
module.exports = new avatar()